using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component.Operation
{
	public class Game3Component : OperationComponent
	{
		private const float MIN_X = -2f;

		private const float MAX_X = 2f;

		private const float MIN_Y = -2.94f;

		private const float MAX_Y = 3.76f;

		private Transform btnBack;

		private List<Transform> efcs = new List<Transform>();

		private Transform wound1;

		private SpriteRenderer wound1Sprite;

		private Transform wound2;

		private SpriteRenderer wound2Sprite;

		private Transform wound3;

		private SpriteRenderer wound3Sprite;

		private Transform wound4;

		private SpriteRenderer wound4Sprite;

		private bool isTouchCottonBall;

		private SpriteRenderer touchWound;

		private Vector2 lastTouchPoint;

		private Transform cottonBall;

		private AudioPlayer audioPlayer;

		private float moveDis;

		private float newX;

		private float newY;

		private void Awake()
		{
			btnBack = base.transform.Find("BtnBack");
			cottonBall = base.transform.Find("CottonBall");
			wound1 = base.transform.Find("Wound1");
			wound1.gameObject.layer = 15;
			wound1Sprite = wound1.GetComponent<SpriteRenderer>();
			wound2 = base.transform.Find("Wound2");
			wound2.gameObject.layer = 15;
			wound2Sprite = wound2.GetComponent<SpriteRenderer>();
			wound3 = base.transform.Find("Wound3");
			wound3.gameObject.layer = 15;
			wound3Sprite = wound3.GetComponent<SpriteRenderer>();
			wound4 = base.transform.Find("Wound4");
			wound4.gameObject.layer = 15;
			wound4Sprite = wound4.GetComponent<SpriteRenderer>();
			List<int> list = new List<int>();
			list.Add(0);
			list.Add(1);
			list.Add(2);
			list.Add(3);
			List<int> list2 = list;
			int count = list2.Count;
			for (int i = 0; i < count; i++)
			{
				Transform transform = base.transform.Find("Wound" + (i + 1));
				if (transform != null)
				{
					int index = Random.Range(0, list2.Count);
					transform.SetLocalXY(GetWoundPosition(list2[index]));
					transform.SetLocalRotationZ(Random.Range(-45f, 45f));
					list2.RemoveAt(index);
				}
			}
		}

		private Vector2 GetWoundPosition(int index)
		{
			switch (index)
			{
			case 0:
				return new Vector2(Random.Range(-1.6f, -0.8f), Random.Range(0.7f, 3f));
			case 1:
				return new Vector2(Random.Range(1f, 1.7f), Random.Range(0.7f, 3f));
			case 2:
				return new Vector2(Random.Range(-1.6f, -0.8f), Random.Range(-1.8f, 0.3f));
			case 3:
				return new Vector2(Random.Range(1f, 1.7f), Random.Range(-1.8f, 0.3f));
			default:
				return Vector2.zero;
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			if (displayer == null)
			{
				return;
			}
			base.TouchDown(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 1);
			touchWound = null;
			isTouchCottonBall = false;
			if (raycastHit2D.collider != null)
			{
				if (raycastHit2D.collider.transform == btnBack)
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
					displayer.ChangeType(OperationDisplayerItem.Type.Main);
				}
				else if (raycastHit2D.collider.transform == cottonBall)
				{
					isTouchCottonBall = true;
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				}
			}
			lastTouchPoint = touchEventData.curPos;
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			base.TouchMove(touchEventData);
			if (isTouchCottonBall)
			{
				touchWound = null;
				RaycastHit2D raycastHit2D = Physics2D.Raycast(cottonBall.transform.position, Vector2.zero, 15f, 32768);
				if (raycastHit2D.collider != null)
				{
					if (raycastHit2D.collider.transform == wound1)
					{
						if (wound1Sprite.color.a > 0f)
						{
							touchWound = wound1Sprite;
							PlayWipeSound();
						}
					}
					else if (raycastHit2D.collider.transform == wound2)
					{
						if (wound2Sprite.color.a > 0f)
						{
							touchWound = wound2Sprite;
							PlayWipeSound();
						}
					}
					else if (raycastHit2D.collider.transform == wound3)
					{
						if (wound3Sprite.color.a > 0f)
						{
							touchWound = wound3Sprite;
							PlayWipeSound();
						}
					}
					else if (raycastHit2D.collider.transform == wound4)
					{
						if (wound4Sprite.color.a > 0f)
						{
							touchWound = wound4Sprite;
							PlayWipeSound();
						}
					}
					else
					{
						RemoveAudioPlayer();
					}
				}
				else
				{
					RemoveAudioPlayer();
				}
				if (touchWound != null)
				{
					moveDis = Vector2.Distance(touchEventData.curPos, lastTouchPoint) / 10f;
					touchWound.SetSpriteAlpha(touchWound.color.a - moveDis);
					if (touchWound.color.a <= 0f)
					{
						Singleton<AudioManager>.Instance.PlaySound("sound_blink");
						Transform transform = Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", touchWound.transform.position + new Vector3(0f, 0f, -1E-05f), 3f);
						transform.SetParent(base.transform);
						efcs.Add(transform);
						touchWound = null;
					}
				}
				newX = cottonBall.localPosition.x + touchEventData.curPos.x - lastTouchPoint.x;
				newY = cottonBall.localPosition.y + touchEventData.curPos.y - lastTouchPoint.y;
				if (newX < -2f)
				{
					newX = -2f;
				}
				else if (newX > 2f)
				{
					newX = 2f;
				}
				if (newY < -2.94f)
				{
					newY = -2.94f;
				}
				else if (newY > 3.76f)
				{
					newY = 3.76f;
				}
				cottonBall.SetLocalXY(newX, newY);
			}
			lastTouchPoint = touchEventData.curPos;
		}

		private void PlayWipeSound()
		{
			if (audioPlayer == null)
			{
				audioPlayer = Singleton<AudioManager>.Instance.CreateAudioPlayer(base.transform);
				if (audioPlayer != null)
				{
					Singleton<AudioManager>.Instance.PlayAudio("sound_wipe_1", true, audioPlayer);
				}
			}
		}

		private void RemoveAudioPlayer()
		{
			if (audioPlayer != null)
			{
				Singleton<AudioManager>.Instance.RemoveAudio(audioPlayer);
				audioPlayer = null;
			}
		}

		private void OnDestroy()
		{
			for (int i = 0; i < efcs.Count; i++)
			{
				if (efcs[i] != null)
				{
					Singleton<GamePoolManager>.Instance.Despawn(efcs[i]);
				}
			}
			efcs.Clear();
			RemoveAudioPlayer();
		}
	}
}
